package com.htjx.mall.commons.utils;

import java.math.BigDecimal;
import java.math.BigInteger;

public class Maths {
	/**
	 * @描述:求组合C(n,r)
	 */
	public static final int getCombinationCount(int n, int r) {
		if (r > n)
			return 0;
		if (r < 0 || n < 0)
			return 0;
		return getFactorial(n).divide(getFactorial(r), BigDecimal.ROUND_HALF_DOWN).divide(getFactorial((n - r)), BigDecimal.ROUND_HALF_DOWN).intValue();
	}

	/**
	 * @描述:求n的阶乘
	 * @param n
	 * @return
	 */
	public static final BigDecimal getFactorial(int num) {
		BigDecimal sum = new BigDecimal(1.0);
		for (int i = 1; i <= num; i++) {
			BigDecimal a = new BigDecimal(new BigInteger(i + ""));
			sum = sum.multiply(a);
		}
		return sum;
	}

}
